import 'dart:io';

import 'package:core_package/ui/widget/my_asset_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_app_unionad/core/configuration.dart';
import 'package:flutter_app_unionad/request/partner_ship_request.dart';
import 'package:flutter_app_unionad/ui/basice/base_navigation_height_widget.dart';
import 'package:core_package/core_package.dart';
import 'package:flutter_app_unionad/ui/basice/my_separator_widget.dart';
import 'package:flutter_app_unionad/ui/user_center/user_center_item10_page.dart';
import 'package:flutter_app_unionad/ui/user_center/user_center_item1_pages.dart';
import 'package:flutter_app_unionad/ui/user_center/user_center_item2_page.dart';
import 'package:flutter_app_unionad/ui/user_center/user_center_item3_page.dart';
import 'package:flutter_app_unionad/ui/user_center/user_center_item4_page.dart';
import 'package:flutter_app_unionad/ui/user_center/user_center_item5_page.dart';
import 'package:flutter_app_unionad/ui/user_center/user_center_item6_page.dart';
import 'package:flutter_app_unionad/ui/user_center/widget/abbout_us_widget.dart';
import 'package:flutter_app_unionad/ui/user_center/widget/binding_facility_widget.dart';
import 'package:flutter_app_unionad/view_model/page_manager_view_model.dart';

//安全中心
class UserCenterPage extends StatefulWidget {
  @override
  _UserCenterPageState createState() => _UserCenterPageState();
}

class _UserCenterPageState extends State<UserCenterPage> with AutomaticKeepAliveClientMixin {
  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    // if(Configuration().phone.stringIsEmpty){
    PartnerShipRequest.getConfigBykey(1).then((value) {
      if (value != null && mounted) {
        Configuration().phone = value;
        setState(() {});
      }
    });
    // }
  }

  @override
  Widget build(BuildContext context) {
    return BaseNavigationHeightWidget(
      navHeight: 220.px,
      padding: EdgeInsets.only(left: 10.px, top: 60.px, right: 10.px),
      needScrollView: false,
      child: buildContent(),
    );
  }

  Widget buildContent() {
    if (Configuration().phone.stringIsEmpty) {
      return ProgressDialog.showFirstLoading();
    }
    return Column(
      children: [
        buildUserInfo(),
        MySizeBox(height: 30),
        Expanded(child: buildListContent()),
        MySizeBox(height: 10),
      ],
    );
  }

  Widget buildListContent() {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 33.px, vertical: 30.px),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(5.px),
      ),
      child: ListView.builder(
        padding: EdgeInsets.zero,
        itemCount: _UserInfoType.getIconList().length,
        itemBuilder: (ctx, index) {
          return buildItem(index);
        },
      ),
    );
  }

  Widget buildItem(int index) {
    bool showLine = false;
    if (_UserInfoType.icon11 == _UserInfoType.getIconList()[index] ||
        _UserInfoType.icon10 == _UserInfoType.getIconList()[index]) {
      showLine = true;
    }

    return Column(
      children: [
        ShowTitleDetailListWidget(
          onTap: () {
            itemOnTop(index);
          },
          padding: EdgeInsets.only(
            left: 5.px,
            top: 18.px,
            bottom: 10.px,
            right: 5.px,
          ),
          titleWidget: Row(
            children: [
              Container(
                alignment: Alignment.centerLeft,
                width: 38.px,
                child: MyAssetImage(
                  _UserInfoType.getIconList()[index],
                  width: _UserInfoType.getSize()[index].width.px,
                  height: _UserInfoType.getSize()[index].height.px,
                ),
              ),
              Text(
                '${_UserInfoType.getTitleList()[index]}${_UserInfoType.getTitleList()[index] == '联系我们' ? ' ${Configuration().phone}' : ""}',
                style: TextStyle(
                  color: MyColors.formHex("#272727ff"),
                  fontSize: MyFont.font14,
                  fontWeight: MyFontWeight.regular,
                ),
              ),
            ],
          ),
          icon: MyAssetImage(
            'icon_next',
            width: 6.px,
            height: 9.px,
          ),
          showBottomLine: !showLine,
          linePadding: EdgeInsets.zero,
        ),
        Visibility(
          visible: showLine,
          child: MySeparator(height: 2, color: MyColors.color_217_217_217),
        ),
      ],
    );
  }

  Widget buildUserInfo() {
    return Row(
      children: [
        MySizeBox(width: 12),
        MyAssetImage("icon_logo_3", width: 60.px, height: 60.px, radius: 5.px),
        MySizeBox(width: 11),
        Column(
          children: [
            Text(
              "小汼汼",
              style: TextStyle(
                color: MyColors.formHex("#272727FF"),
                fontWeight: MyFontWeight.regular,
                fontSize: MyFont.font13,
              ),
            ),
            MySizeBox(height: 5),
            Text(
              getString(),
              style: TextStyle(
                color: MyColors.formHex("#272727FF"),
                fontWeight: MyFontWeight.bold,
                fontSize: MyFont.font16,
              ),
            ),
          ],
        )
      ],
    );
  }

  String getString() {
    var time = DateTime.now();
    if (time.hour >= 0 && time.hour <= 4) {
      return '深夜了';
    }
    if (time.hour >= 5 && time.hour <= 7) {
      return '早上好';
    }
    if (time.hour >= 7 && time.hour <= 11) {
      return '上午好';
    }
    if (time.hour >= 12 && time.hour <= 18) {
      return '下午好';
    }
    return '晚上好';
  }

  void itemOnTop(int index) {
    print(_UserInfoType.getIconList()[index]);
    if (_UserInfoType.getIconList()[index] == _UserInfoType.icon3) {
      //关于我们
      ShowCenterAlertDialog.showAlert(context, builder: (ctx) {
        return AboutUsWidget();
      }, width: 335.px, height: 150.px);
    } else if (_UserInfoType.getIconList()[index] == _UserInfoType.icon5) {
      //绑定设备
      ShowCenterAlertDialog.showAlert(context, builder: (ctx) {
        return BindingFacilityWidget();
      }, width: 335.px, height: 210.px);
    } else if (_UserInfoType.getIconList()[index] == _UserInfoType.icon4) {
      //联系我们
      Configuration().call();
    } else if (_UserInfoType.getIconList()[index] == _UserInfoType.icon8) {
      // 我的设备
      NavigatorUtil.push(context, UserCenterItem2Page.routeName);
    } else if (_UserInfoType.getIconList()[index] == _UserInfoType.icon9) {
      // 安卓检查更新
    } else if (_UserInfoType.getIconList()[index] == _UserInfoType.icon10) {
      NavigatorUtil.push(context, UserCenterItem10Page.routeName).then((value) {
        if (value != null) {
          if (value is String && value == "login") {
            Configuration.loginOut();
            PagesManagerViewModel.updateType(context, PagesManagerType.login);
          }
        }
      });
    } else if (_UserInfoType.getIconList()[index] == _UserInfoType.icon2) {
      NavigatorUtil.push(context, UserCenterItem1Page.routeName);
    } else if (_UserInfoType.getIconList()[index] == _UserInfoType.icon11) {
      NavigatorUtil.push(context, UserCenterItem5Page.routeName);
    } else if (_UserInfoType.getIconList()[index] == _UserInfoType.icon6) {
      NavigatorUtil.push(context, UserCenterItem6Page.routeName);
    }
  }
}

class _UserInfoType {
  static const String icon1 = "icon_icon1";
  static const String icon2 = "icon_icon2";
  static const String icon3 = "icon_icon3";
  static const String icon4 = "icon_icon4";
  static const String icon5 = "icon_icon5";
  static const String icon11 = "wifi";

  static const String icon6 = "icon_icon6";
  static const String icon7 = "icon_icon7";
  static const String icon8 = "icon_icon8";
  static const String icon9 = "icon_icon9";
  static const String icon10 = "icon_icon10";

  static List<String> getIconList() {
    List<String> icons = [
      _UserInfoType.icon8,
      // _UserInfoType.icon1,
      _UserInfoType.icon2,
      _UserInfoType.icon3,
      _UserInfoType.icon4,
      _UserInfoType.icon5,
      _UserInfoType.icon11,
      _UserInfoType.icon6,
      // _UserInfoType.icon7,
      // _UserInfoType.icon8,
      // _UserInfoType.icon9,
      _UserInfoType.icon10,
    ];

    // if (Platform.isIOS) {
    //   icons.removeAt(9);
    // }
    return icons;
  }

  static List<String> getTitleList() {
    List<String> titleList = [
      '我的设备',
      // '滤芯维护记录',
      '充值、兑换、提现记录',
      '关于我们',
      '联系我们',
      '绑定设备',
      '设置设备链接',
      '预约安装',
      // "帮助中心",
      // '给汼汼生活打分',
      // '检查更新',
      '安全中心'
    ];
    // if (Platform.isIOS) {
    //   titleList.removeAt(9);
    // }
    return titleList;
  }

  static List<String> routeNames() {
    List<String> icons = [];
    for (int i = 0; i < 11; i++) {
      if ((Platform.isIOS && i != 9) || Platform.isAndroid) icons.add("/userCenterItem${i}Page");
    }
    return icons;
  }

  static List<Size> getSize() {
    List<Size> titleList = [
      Size(16, 16),
      // Size(16, 15),
      Size(16, 12),
      Size(17, 17),
      Size(13, 17),
      Size(18, 13),
      Size(18, 18),
      Size(17, 17),
      Size(17, 17),
      // Size(16, 15),
      // Size(16, 17),
      Size(13, 16)
    ];
    // if (Platform.isIOS) {
    //   titleList.removeAt(9);
    // }
    return titleList;
  }
}
